﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;

namespace MEMDB
{
    public partial class CreateDatabase : Form
    {
        SQLConnection m_conn;
        bool isSuccessful = false;
        public CreateDatabase(SQLConnection conn)
        {
            InitializeComponent();
            m_conn = conn;
        }

        public  bool Successful
        {
            get
            {
                return isSuccessful;
            }
        }

        private void textBox1_TextChanged(object sender, EventArgs e)
        {

        }

        private void label1_Click(object sender, EventArgs e)
        {

        }

        private void CreateDatabase_Load(object sender, EventArgs e)
        {

        }

        private void button2_Click(object sender, EventArgs e)
        {
            this.Close();
        }

        private void CreateDBButton_Click(object sender, EventArgs e)
        {
            string strCommand = "create database ";
            if(this.DBNametextbox.Text == "")
            {
                System.Windows.Forms.MessageBox.Show("未指明数据库名！");
                return;
            }

            strCommand += DBNametextbox.Text + " max_size " + DBSizenumericUpDown1.Value.ToString() + " on '";

            if(DBFilePathtextbox.Text == "")
            {
                System.Windows.Forms.MessageBox.Show("未指明数据库文件路径！");
                return;
            }

            strCommand += DBFilePathtextbox.Text + "' log on '";

            if(DBLogfilePathTextBox.Text == "")
            {
                System.Windows.Forms.MessageBox.Show("未指明数据库日志文件路径！");
                return;
            }

            strCommand += DBLogfilePathTextBox.Text + "' user = '";

            if(UsertestBox.Text == "")
            {
                System.Windows.Forms.MessageBox.Show("未指明用户名！");
                return;
            }

            strCommand += UsertestBox.Text + "' password = '";

            if(PasswordTextBox.Text == "")
            {
                System.Windows.Forms.MessageBox.Show("请输入您的密码。3Q");
                return;
            }

            strCommand += PasswordTextBox.Text + "';";

            SQLCommand cmd = new SQLCommand(strCommand, m_conn);

            try
            {
                cmd.ExecuteNonQuery();
            }
            catch(SQLException ex)
            {
                System.Windows.Forms.MessageBox.Show(ex.Message);
                return;
            }

           // System.Windows.Forms.MessageBox.Show("创建数据库成功!");

            isSuccessful = true;
            this.Close();
        }
    }
}
